﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.WorldArtPrice.Entities;
using System.Data.SqlClient;
using System.Data;

namespace BusinessLayer.WorldArtPrice
{
    public class ImageLastProjectBL
    {
        private readonly DbHelper _db;

        public ImageLastProjectBL()
        {
            _db = new DbHelper(new DbConfig().DbWorldArtPriceConnectionString);         
        }

        public List<ImageLastProject> GetAll()
        {
            var oCommand = new SqlCommand("sp_ImageLastProject_Select_All") { CommandType = CommandType.StoredProcedure };
            return _db.GetList<ImageLastProject>(oCommand);
        }

        public ImageLastProject GetById(int id)
        {
            var oCommand = new SqlCommand("sp_ImageLastProject_Select_By_Id") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", id));
            return _db.GetList<ImageLastProject>(oCommand).FirstOrDefault();
        }

        public List<ImageLastProject> GetByType(int imageProjectType)
        {
            var oCommand = new SqlCommand("sp_ImageLastProject_Select_By_Type") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Type", imageProjectType));
            return _db.GetList<ImageLastProject>(oCommand);
        }

        public ImageLastProject GetTop1RandomByType(int imageProjectType)
        {
            var oCommand = new SqlCommand("sp_ImageLastProject_Select_Top1_Random_By_Type") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Type", imageProjectType));
            return _db.GetList<ImageLastProject>(oCommand).FirstOrDefault();
        }

        public void Insert(ImageLastProject imageLastProject)
        {
            var oCommand = new SqlCommand("sp_ImageLastProject_Insert") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Image", imageLastProject.Image));
            oCommand.Parameters.Add(new SqlParameter("@ImageUrl", imageLastProject.ImageUrl));
            oCommand.Parameters.Add(new SqlParameter("@Type", imageLastProject.Type));
            oCommand.Parameters.Add(new SqlParameter("@RedirectUrl", imageLastProject.RedirectUrl));
            oCommand.Parameters.Add(new SqlParameter("@Info", imageLastProject.Info));
            
            _db.ExecuteNonQuery(oCommand);
        }

        public void Update(ImageLastProject imageLastProject)
        {
            var oCommand = new SqlCommand("sp_ImageLastProject_Update") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", imageLastProject.Id));
            oCommand.Parameters.Add(new SqlParameter("@Image", imageLastProject.Image));
            oCommand.Parameters.Add(new SqlParameter("@ImageUrl", imageLastProject.ImageUrl));
            oCommand.Parameters.Add(new SqlParameter("@Type", imageLastProject.Type));
            oCommand.Parameters.Add(new SqlParameter("@RedirectUrl", imageLastProject.RedirectUrl));
            oCommand.Parameters.Add(new SqlParameter("@Info", imageLastProject.Info));

            _db.ExecuteNonQuery(oCommand);
        }

        public void Delete(int id)
        {
            var oCommand = new SqlCommand("sp_ImageLastProject_Delete") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", id));            

            _db.ExecuteNonQuery(oCommand);
        }
    }
}
